package LeetCode;

public class LC_367_ValidPerfectSquare {

    public static void main(String[] args) {

    }

    public class Solution_1 {
        public boolean isPerfectSquare(int num) {
            if (num < 0) return false;
            if (num == 1) return true;
            for (int i = 1; i <= num / i; i++) {
                if (i * i == num) return true;
            }
            return false;
        }
    }


    public class Solution_2 {
        public boolean isPerfectSquare(int num) {
            if (num < 0) return false;
            if (num == 1) return true;
            int low = 0, high = num / 2;
            while (low <= high) {
                long mid = (low + high) / 2;
                long tmp = mid * mid;
                if (tmp == num) return true;
                if (tmp > num) high = (int) (mid - 1);
                else low = (int) (mid + 1);
            }
            return false;
        }
    }


    public class Solution_3 {
        public boolean isPerfectSquare(int num) {
            int i = 1;
            while (num > 0) {
                num -= i;
                i += 2;
            }
            return num == 0;
        }
    }
}